// SlagCar/OrderStatistics/OrderStatistics.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    activeNames: [],  //折叠面板
    deliveryList: [],  //统计工地列表

    quickFlag: false,

    searchList:[
      {field_id: 'type',field_name: '统计',type: 'picklist',
        dfltVal: 'delivery',
        optionList: [
          {name: '工地',id: 'delivery'},
          {name: '消纳场',id: 'destination'},
          {name: '司机',id: 'driver'},
        ],optionId: 'id',optionName: 'name'},
      {field_id: 'statistical_cycle',field_name: '统计时间',type: 'datetime',
        dfltdate: 'today',
        dateList: [
          {id: 'today',name: '今天'},
          {id: 'yesterday',name: '昨天'},
          {id: 'the_day_before_yesterday',name: '前日'},
          {id: 'this_week',name: '本周'},
          // {id: 'last_week',name: '上周'},
          {id: 'this_month',name: '本月'},
          // {id: 'last_month',name: '上月'},
          {id: 'this_season',name: '本季'},
          // {id: 'last_season',name: '上季'},
          {id: 'this_year',name: '本年'},
          // {id: 'last_year',name: '上年'},
          ]},
      {field_id: 'is_auditing_1',field_name: '审核',type: 'picklist',
        dfltVal: false,
        optionList: [
          {name: '未审核',id: false},
          {name: '已审核',id: true},
        ],optionName: 'name',optionId: 'id' },
    ],
    searchObj: {
      statistical_cycle: 'today',
      is_auditing_1: false
    },
    filterObj: {
      type: 'delivery',
    },

    list: [],

    filterText: ['工地','今天','未审核'],
    typeText: '工地',
    timeText: '今天',
    approveText: '未审核',
  },

  //获取统计列表
  getOrderCount(){
    let url = 'ZTAuth/muck_order_complete_count_driver_lv1',
      {driver_end_time_start,driver_end_time_end,userType} = this.data,
      post = {
        "driver_end_time_start": '2023-10-1',
        "driver_end_time_end": '2023-11-4',
        "is_all": userType !== 'driver'
      }
    wx.showLoading({mask: true});
    wx.$post(5,url,post).then(res => {
      if (res.errcode != 0) return
      wx.hideLoading();
      let data = res.data.row || [];
      this.setData({deliveryList: data,activeNames: []})
    })
  },
  getOrderCountDtl(id){
    return new Promise(resolve => {
      wx.showLoading({mask: true})
      let url = 'ZTAuth/muck_order_complete_count_driver_lv2',
        {driver_end_time_start,driver_end_time_end,userType} = this.data,
        post = {
          "driver_end_time_start": '2023-10-1',
          "driver_end_time_end": '2023-11-4',
          "reference1_all_delivery": id,
          "is_all": userType !== 'driver'
        };
      wx.$post(5,url,post).then(res => {
        if (res.errcode != 0) return;
        wx.hideLoading();
        let data = res.data.row || [];
        resolve(data);
      })
    })
  },


  //显示搜索
  showQuick(){
    this.setData({
      quickFlag: !this.data.quickFlag,
    })
  },
  searchChange({detail}){
    let {val,field_id} = detail,
      {searchObj,filterObj} = this.data,dataObj = {...filterObj};
    // if (!val) return;
    if (field_id === 'statistical_cycle') {
      if (typeof val === 'object'){
        let {startDate,endDate} = val;
        dataObj = {...dataObj,datetime_s: startDate || endDate,datetime_e: endDate || startDate,[field_id]: ''}
      }else {
        delete dataObj.datetime_s;
        delete dataObj.datetime_e;
        dataObj = {...dataObj,[field_id]: val}
      }
    }else {
      dataObj[field_id] = val;
    }
    this.setData({filterObj: dataObj})
  },
  searchData({detail}){
    let {searchObj,filterObj,typeText,timeText,approveText} = this.data,
      obj = {},
      filterText = [],
      {type,statistical_cycle,datetime_s,datetime_e,is_auditing_1} = filterObj;
    if (type) {
      let obj = {
        'delivery': '工地',
        'destination': '消纳场',
        'driver': '司机',
      };
      typeText = obj[type];
    }
    if (statistical_cycle){
      let obj ={
       'today': '今天',
       'yesterday': '昨天',
       'the_day_before_yesterday': '前日',
       'this_week': '本周',
       'last_week': '上周',
       'this_month': '本月',
       'last_month': '上月',
       'this_season': '本季',
       'last_season': '上季',
       'this_year': '本年',
       'last_year': '上年',
      };
      timeText = obj[statistical_cycle];
      searchObj.statistical_cycle = statistical_cycle;
    }
    if (datetime_s || datetime_e){
      timeText = `${datetime_s}-${datetime_e}`
      searchObj.datetime_s = datetime_s;
      searchObj.datetime_e = datetime_e;
      searchObj.statistical_cycle = 'Custom'
    }
    if (is_auditing_1 !== undefined){
      approveText = is_auditing_1 ? '已审核' : '未审核';
      searchObj.is_auditing_1 = is_auditing_1;
    }

    this.setData({typeText,timeText,approveText,searchObj});

    this.getData();
    console.log(searchObj);
  },

  //查询数据
  getData(){
    let { searchObj,filterObj } = this.data,
      post = {...searchObj,is_all: true },
      url = '';
    if (filterObj.type === 'delivery') url = 'ZTAuth/muck_waybill_statistics_delivery';
    if (filterObj.type === 'destination') url = 'ZTAuth/muck_waybill_statistics_destination';
    if (filterObj.type === 'driver') url = 'ZTAuth/muck_waybill_statistics_driver';
    delete post.type;
    wx.showLoading({mask: true})
    wx.$post(5,url,post,).then(res => {
      if (res.errcode != 0 )return;
      let list = res.data.row || [];
      this.setData({list});
      wx.hideLoading();
    })
  },
  bindClick(e){
    let {rid} = e.currentTarget.dataset,
      { filterObj,searchObj } = this.data,
      {is_auditing_1,statistical_cycle,datetime_s,datetime_e} = searchObj,
      where = [];

    where.push(
      {
      "CompareWith":"muck_waybill_main.driver_end_time",
      "Operator":"is not null",
      "CompareValue": '',
      "or":0
      },
      {
        "CompareWith":"muck_waybill_main." + (filterObj.type === 'delivery' ? 'reference1_all_delivery' : filterObj.type === 'destination' ? 'reference1_all_destination' : 'reference1_driver'),
        "Operator":"=",
        "CompareValue": rid,
        "or":0
      }
    );

    if (!is_auditing_1){
      where.push(
        {
          "CompareWith":"muck_waybill_main.is_auditing_1",
          "Operator":"=",
          "CompareValue": false,
          "or":0
        },
        /*{
          "CompareWith":"muck_waybill_main.auditing_1_time",
          "Operator":"is not null",
          "CompareValue": "",
          "or":0
        },
        {
          "CompareWith":"muck_waybill_main.auditing_1_user_row_id",
          "Operator":"is null",
          "CompareValue": "",
          "or":0
        },*/
      )
    }else {
      where.push(
        {
          "CompareWith":"muck_waybill_main.is_auditing_1",
          "Operator":"=",
          "CompareValue": true,
          "or":0
        },
      )
    }

    switch (statistical_cycle) {
      case 'today':
        where.push({
          CompareValue: 1,
          CompareWith: "dbo.FUN_query_add_day(0,muck_waybill_main.driver_end_time)",
          Operator: "=",
          "or":0
        });
        break;
      case 'yesterday':
        where.push({
          CompareValue: 1,
          CompareWith: "dbo.FUN_query_add_day(-1,muck_waybill_main.driver_end_time)",
          Operator: "=",
          "or":0
        });
        break;
      case 'the_day_before_yesterday':
        where.push({
          CompareValue: 1,
          CompareWith: "dbo.FUN_query_add_day(-2,muck_waybill_main.driver_end_time)",
          Operator: "=",
          "or":0
        });
        break;
      case 'this_week':
        where.push({
          CompareValue: 1,
          CompareWith: "dbo.FUN_query_add_day(-7,muck_waybill_main.driver_end_time)",
          Operator: "=",
          "or":0
        });
        break;
      case 'this_month':
        where.push({
          CompareValue: 0,
          CompareWith: "datediff(month,muck_waybill_main.driver_end_time,getdate())",
          Operator: "=",
          "or":0
        });
        break;
      case 'this_season':
        where.push({
          CompareValue: "datepart(quarter,GETDATE())",
          CompareWith: "datepart(quarter,muck_waybill_main.driver_end_time)",
          Operator: "=",
          "or":0
        });
        break;
      case 'this_year':
        where.push({
          CompareValue: "datepart(year,GETDATE())",
          CompareWith: "datepart(year,muck_waybill_main.driver_end_time)",
          Operator: "=",
          "or":0
        });
        break;
      case 'Custom':
        where.push(
          {
            CompareValue: datetime_s + ' 00:00',
            CompareWith: "muck_waybill_main.driver_end_time",
            Operator: ">=",
            "or":0
          },
          {
            CompareValue: datetime_e + ' 23:59:59',
            CompareWith: "muck_waybill_main.driver_end_time",
            Operator: "<=",
            "or":0
          },
          )
      break;
    }

    wx.setStorageSync('approveQuery',where);
    wx.navigateTo({
      url: '/SlagCar/ApproveList/ApproveList'
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData();

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
